<style type="text/css">
<!--
body {
	background-color: #FFFFFF;
}
-->
</style><body><?php
     include("../../MerchandiseBay/conn.php");
	 function isGreaterThan($inputDay, $inputMon, $inputYear)
	 {
		date_default_timezone_set('Asia/Kuala_Lumpur');
		$_sDayNow = (int)date("d");
		$_sMonthNow = (int)date("m");
		$_sYearNow = (int)date("Y");
		if($inputYear > $_sYearNow)
		{
			return true;
		}
		else
		{
			if($inputMon > $_sMonthNow)
			{
				return true;
			}
			else
			{
				if($inputDay >= $_sDayNow)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		
	 }
		$sDate = $_POST['reservationdate'];
		$sTime = $_POST['time'];
		if(strLen($Time) > 10 || strLen($sDate) > 10 || strLen($sDate) < 10 || strstr($sDate, '#') || strstr($sDate,"'") || strstr($sDate,'"'))
		{//simple input checking/filtering
			echo "<script type='text/javascript'>
			window.alert('Invalid value. Please try again.');
			window.location.assign('reservationForm.php');
			</script>
			";
			return;
		}
		date_default_timezone_set('Asia/Kuala_Lumpur');
		$sDayNow = (int)date("d");
		$sMonthNow = (int)date("m");
		$sYearNow = (int)date("Y");
		$sTimeNow = (int)date("H");
		$aBookDate = explode("/",$sDate);//m,d,y
		
		//if(((int)$aBookDate[1]>=$sDayNow && (int)$aBookDate[0] >=$sMonthNow && (int)$aBookDate[2]>=$sYearNow))
		if(isGreaterThan((int)$aBookDate[1],(int)$aBookDate[0],(int)$aBookDate[2]))
		{
			
			$sBookTime = (int)substr($sTime, 0, 2);
			$sBookAMPM = substr($sTime, 2, 2);
			if($sBookAMPM == 'pm')
			{
				$sBookTime += 12;
			}
			$iMinHour = 1;
			if(((int)$aBookDate[1]==$sDayNow && (int)$aBookDate[0] ==$sMonthNow && (int)$aBookDate[2]==$sYearNow))
			{//today
//				if(($sTimeNow - $iMinHour) > $sBookTime)
				if(($sBookTime - ($iMinHour+1)) < $sTimeNow) 
				{
					echo "<script type='text/javascript'>
					window.alert('You must reserve minimum $iMinHour hours before the reservation time.');
					window.location.assign('reservationForm.php');
					</script>
					";
					return;
				}
			}
		}
		else
		{
			echo "<script type='text/javascript'>
			window.alert('Invalid date.');
			window.location.assign('reservationForm.php');
			</script>
			";
			return;
		}

		//query if reservation is available
		$sQueryAvailable = "SELECT * FROM `MBay`.`reservation` where reservationTime = '".$sTime."' and reservationDate = '".$sDate."'";
	$queryAvailable = mysql_query($sQueryAvailable, $conn);
	if($queryAvailable)
	{
		$iTotalReserved = mysql_num_rows($queryAvailable);	
		$iTotalAvailable = 5;
		if($iTotalReserved > $iTotalAvailable)
		{//exceed total available
			echo "<script type='text/javascript'>
			window.alert('Reservation for $sTime at $sDate is not available. Please select another time.');
			window.location.assign('reservationForm.php');
			</script>
			";
			return;
		}
	}
	else
	{
		die (mysql_error());
		return;
	}

    echo  "<script language=javascript>
          //window.alert('Reservation is available. To confirm reservation, please fill in your details.');
           window.location.assign('selectmenu.php?time=$sTime&date=$sDate');
		   </script>";	
	?>
